package com.boxing.dao;

import com.boxing.beans.User;
import com.boxing.beans.UserInfo;
import com.boxing.beans.UserInfoT;

public class UserDao extends BaseDao{

    public boolean login(User user) {
        User userResult = queryOne("select user,email from t_user where email=? and psd=?", User.class, user.getEmail(), user.getPsd());
        return userResult != null && userResult.getEmail() != null && userResult.getEmail().length() > 0;
    }

    public boolean register(UserInfo userInfo) {
        int i = insertData("insert into t_user(user,psd,email) values(?,?,?)", userInfo.getUser(), userInfo.getPsd(), userInfo.getEmail());
        return i > 0;
    }

    public UserInfoT getUserInfo(String userEmail){
        return queryOne("select a.user,b.email,b.age,b.icon from t_user a,t_user_info b where a.email=?", UserInfoT.class, userEmail);
    }

    public UserInfo unionEmail(String email){
        return queryOne("select user,psd,email from t_user where email=?", UserInfo.class, email);
    }

    public boolean setIcon(UserInfoT userInfoParam) {
        // 如果该用户没有头像，则插入数据； 如果该用户有头像，则更新数据
        UserInfoT userInfoT = queryOne("select email,icon,age from t_user_info where email=?", UserInfoT.class, userInfoParam.getEmail());
        int i;
        if (userInfoT == null){
            i = insertData("insert into t_user_info(email, icon) values(?,?)", userInfoParam.getEmail(), userInfoParam.getIcon());
        }else {
            i = insertData("update t_user_info set icon=? where email=?", userInfoParam.getIcon(), userInfoParam.getEmail());
        }
        return i>0;
    }
}
